package org.basis.algorithm.list.common;

import lombok.Data;

import java.util.Objects;

/**
 * 单链表节点
 *
 * @author Mr_wenpan@163.com 2021/12/08 16:57
 */
@Data
public class SingleNode<T extends Comparable<T>> {
    public T data;
    public SingleNode<T> next;

    public SingleNode() {
    }

    public SingleNode(T data) {
        this.data = data;
    }

    /**
     * 为了适配有环的情况，所以需要重写toString，equals, hashCode, 不然打印的时候会报错stack overflow
     * toString中不能打印next
     */
    @Override
    public String toString() {
        return "SingleNode{" +
                "data=" + data +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        // 必须是同一个对象是才能为true
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        return false;
    }

    @Override
    public int hashCode() {
        // 只对data求hash
        return Objects.hash(data);
    }
}
